﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using System.IO;
using EnvDTE;

namespace WizardExtension
{
    public class BLExtension : ExtensionBase, IWizard
    {
        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {
            
        }

        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
            string className = projectItem.Name;
            if (className.Contains("."))
                className = className.Substring(0, className.IndexOf("."));

            bool found = false;

            foreach (ProjectItem pi in projectItem.ContainingProject.ProjectItems)
            {
                for (short i = 0; i < pi.FileCount; i++)
                {
                    string filePath = pi.FileNames[i];

                    if (filePath.EndsWith("AccessPoint\\"))
                    {
                        found = true;

                        AddBLImplementationPropertyCode(filePath + "BusinessLogicAccessPoint.cs", className);

                        AddBLInterfacePropertyCode(filePath + "IBusinessLogicAccessPoint.cs", className);
                    }
                }
            }

            if (!found)
                MessageBox.Show("Unable to find Access Point Class and Interface!" + Environment.NewLine + "Please add the new Type properties manually.");
        }

        private void AddBLImplementationPropertyCode(string filePath, string className)
        {
            string fileContent = File.ReadAllText(filePath);
            string propertyContent = Environment.NewLine + "        public " + className + " " + className + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            get" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                return GetSharedInstance<" + className + ">() as " + className + ";" + Environment.NewLine +
                "            }" + Environment.NewLine +
                "        }";

            int bracesCount = fileContent.Count(ch => ch == '}');
            int c = 0;
            for (int i = 0; i < fileContent.Length; i++)
                if (fileContent[i] == '}')
                {
                    c++;

                    if (c == (bracesCount - 1))
                    {
                        fileContent = fileContent.Substring(0, i - 1) + propertyContent + Environment.NewLine + "    }" + Environment.NewLine + "}";
                    }
                }

            File.WriteAllText(filePath, fileContent);
        }

        private void AddBLInterfacePropertyCode(string filePath, string className)
        {
            string fileContent = File.ReadAllText(filePath);
            string propertyContent = "     " + className + " " + className + " { get; }";

            int bracesCount = fileContent.Count(ch => ch == '}');
            int c = 0;
            for (int i = 0; i < fileContent.Length; i++)
                if (fileContent[i] == '}')
                {
                    c++;

                    if (c == (bracesCount - 1))
                    {
                        fileContent = fileContent.Substring(0, i - 1) + propertyContent + Environment.NewLine + "    }" + Environment.NewLine + "}";
                    }
                }

            File.WriteAllText(filePath, fileContent);
        }

        public void RunFinished()
        {

        }

        private Dictionary<string, string> _replacementsDictionary;

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            _replacementsDictionary = replacementsDictionary;
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;

            //ShowMessage(_replacementsDictionary);

            //string path = Path.GetPathRoot(filePath);
            //string file1 = path + "\\AccessPoint\\BusinessLogicAccessPoint.cs";
            //string file2 = path + "\\AccessPoint\\IBusinessLogicAccessPoint.cs";

            //MessageBox.Show(filePath);
            //MessageBox.Show(file2);

            //bool result = File.Exists(file1) && File.Exists(file2);
            //if (!result)
            //    MessageBox.Show("Unable to access NLayers files (BusinessLogicAccessPoint.cs / IBusinessLogicAccessPoint.cs)!" + Environment.NewLine + "Reason: Invalid Project / Invalid Path");

            //return result;
        }
    }
}

